import sqlite3
from pathlib import Path
'''
SQLite3 Editor 安装sqlite3的插件

'''

# 连接到数据库
conn = sqlite3.connect(Path('.\Python高级\sqlite3数据库操作\example.db'))

## 获取游标
c = conn.cursor()

# 检查表是否存在
c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='stocks'")
table_exists = c.fetchone()

# 如果表不存在，则创建表
if not table_exists:
    c.execute('''CREATE TABLE stocks
                (date text, trans text, symbol text, qty real, price real)''')
for i in range(10000):
    # 插入数据
    c.execute("INSERT INTO stocks VALUES ('2023-11-25', 'BUY', 'AAPL', 1001, 100.5)")
    conn.commit()

    
# #更新数据
# c.execute("UPDATE stocks SET price = 105 WHERE symbol = 'AAPA'")
# conn.commit()

# #删除数据
# c.execute("DELETE FROM stocks WHERE symbol = 'AAPL1'")
# conn.commit()


# 查询数据
c.execute("SELECT * FROM stocks")
rows = c.fetchall()
for row in rows:
    print(row)